<?php

namespace app\admin\controller;

use app\common\model\PurchaseRecord;
use think\Db;

class Purchase extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    /**
     * @route('purchase/productlist')
     * 商店商品列表
     */
    public function productlist()
    {
//        // 获取搜索条件
        $get['product_name'] = input('get.product_name');
////        $get['state'] = input('get.state');
        $where[]=['p.is_del','=',1];
        $where[]=['p.state','=',1];
        $where[]=['p.class_id','=',1];
        if(!empty($get['product_name'])){
            $where[] = ['p.product_name','like',"%".$get['product_name']."%"];
        }
//        // 后台账号数据
        $rs_data = Db::table('ddt_products')
            ->alias('p')
            ->leftjoin('ddt_products_type pt','pt.type_id = p.type_id')
            ->where($where)
            ->field('p.*,pt.type_name')
            ->order('p.product_id desc')
            ->paginate($this->limit);
//            ->buildSql();
//
//        var_dump($rs_data);die;
        $tvar = array(
            "title" => "申购商品列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('purchase/productlist');
    }

    /**
     * @route('purchase/boxpurchase')
     * 商品申购
     */
    public function Boxpurchase()
    {

        $model = new \app\common\model\Products();
        $pmodel = new \app\common\model\Purchase();
        if(request()->isAjax()){
//            var_dump(234);die;
            $product_id = input("post.product_id");
            $product = $model
                ->where('product_id','=',$product_id)
                ->where('is_del','=',1)
                ->find();
            if(!$product){
                $this->error('不存在该申购商品','/purchase/productlist');
            }
            $data = $product->toArray();
            if($this->_shopid != 0){
                $param['type'] = 1;
            } else {
                $param['type'] = 2;
            }
            $param = [
                "product_id" => $data['product_id'],
                "product_name" => $data['product_name'],
                "number" => input("post.number"),
                "admin_id" => $this->_userid,
                "shop_id" => $this->_shopid,
            ];
//            var_dump($param);die;
            $rs = $pmodel->edit($param);
            if($rs === false){
                $this->error($model->getError());
            }
            $this->success('操作成功','/products/shopsproduct');
        }

        $product_id = $_GET['product_id'];
        $tvar = array(
            "title" => "添加商品",
            "time"  => time(),
            "product_id"   => $product_id,
        );

        $this->assign("data", $tvar);
        return $this->fetch('purchase/boxpurchase');
    }

    /**
     * @route('purchase/purchaselist')
     * 申购列表
     */
    public function purchaselist()
    {
        $model = new PurchaseRecord();
//        // 获取搜索条件
        $get['product_name'] = input('get.product_name');
        $get['shop_name'] = input('get.shop_name');
        $get['status_id'] = input('get.status_id');
        $where[] = ['type','=',1];
        if(!empty($get['product_name'])){
            $where[] = ['pr.product_name','like',"%".$get['product_name']."%"];
        }
        if(!empty($get['shop_name'])){
            $where[] = ['s.shop_name','like',"%".$get['shop_name']."%"];
        }
        if(!empty($get['status_id'])){
            $where[] = ['pr.status','=',$get['status_id']];
        }
        $isshop = false;
        if($this->_shopid != 0){
            $where[] = ['pr.shop_id','=',$this->_shopid];
            $isshop = true;
        }
        $statuslist = [
            '1' => '待审核',
            '2' => '审核通过待发货',
            '3' => '已发货',
            '4' => '已收货',
            '5' => '审核不通过',
        ];
//        // 后台账号数据
        $rs_data = $model
            ->alias('pr')
            ->leftjoin('ddt_shops s','pr.shop_id = s.shop_id')
            ->where($where)
            ->field('pr.*,s.shop_name')
            ->order('pr.p_r_id desc')
            ->paginate($this->limit)
            ->each(function($items,$key){
                $status = [
                    '1' => '待审核',
                    '2' => '审核通过待发货',
                    '3' => '已发货',
                    '4' => '已收货',
                    '5' => '审核不通过',
                ];
                $items['status_name'] = $status[$items['status']];
                return $items;
            });
//            ->buildSql();
//
//        var_dump($rs_data);die;
        $tvar = array(
            "title" => "申购管理列表",
            "time"  => time(),
            "data"  => $rs_data,
            "statuslist" => $statuslist,
            "get"   => $get,
            "isshop" => $isshop,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('purchase/purchaselist');
    }

    /**
     * @route('purchase/suredelivery')
     * 确认发货
     */
    public function SureDelivery()
    {
        $purchaseid = input('purchase_id');
        $model = new PurchaseRecord();
        $where['p_r_id'] = $purchaseid;
        $where['status'] = 2;
        $purchase = $model->where($where)->find();
//        var_dump($purchase);die;
        if(!$purchase){
            $this->error('不存在的申购记录');
        }
        $param['status'] = 3;
        $rs2 = $model->edit($param,$purchaseid);
        if($rs2 === false){
            $this->error("操作失败");
        }
        $this->success('操作成功');
    }

    /**
     * @route('purchase/suregoods')
     * 确认发货
     */
    public function SureGoods()
    {
        $purchaseid = input('purchase_id');
        $model = new PurchaseRecord();
        $where['p_r_id'] = $purchaseid;
        $where['status'] = 3;
        $purchase = $model->where($where)->find();
//        var_dump($purchase);die;
        if(!$purchase){
            $this->error('不存在的申购记录');
        }
        $param['status'] = 4;
        $rs2 = $model->edit($param,$purchaseid);
        if($rs2 === false){
            $this->error("操作失败");
        }
        $this->success('操作成功');
    }

    /**
     * @route('purchase/boxcheck')
     * 申请审核
     */
    public function BoxCheck()
    {
        $model = new PurchaseRecord();
        $get['purchase_id'] = input('get.purchase_id');
//        var_dump($get['applyreg_id']);die;
        if(request()->isAjax()){
            $param = request()->param();
            if(empty($param['purchase_id']) || empty($param['status']) || empty($param['reasons'])){
                $this->error('必填项不能为空');
            }
            $data = [
                'status' => $param['status'],
                'reasons' => $param['reasons'],
            ];
//            var_dump($param);die;
            $rs = $model->edit($data,$param['purchase_id']);
            if($rs === false){
                $this->error($model->getError());
            }
            $this->success('操作成功','/purchase/purchaselist');
        }
        $checkdetail = $model->where('p_r_id','=',$get['purchase_id'])->find();
//        var_dump($checkdetail);
        if(!$checkdetail){
            $this->error('不存在的审核','/purchase/purchaselist');
        }
        $tvar = array(
            "title" => "添加账号",
            "time"  => time(),
            "purchase_id" => $get['purchase_id'],
        );
        $this->assign("data", $tvar);
        return $this->fetch('purchase/boxcheckpurchase');
    }
}